package com.mbyte.easy.rest.maintainrecord;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Maintainrecord;
import com.mbyte.easy.admin.service.IMaintainrecordService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@RestController
@RequestMapping("rest/maintainrecord")
public class RestMaintainrecordController extends BaseController  {

    @Autowired
    private IMaintainrecordService maintainrecordService;

    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param maintainrecord
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String equUpdatetimeSpace, Maintainrecord maintainrecord) {
        Page<Maintainrecord> page = new Page<Maintainrecord>(pageNo, pageSize);
        QueryWrapper<Maintainrecord> queryWrapper = new QueryWrapper<Maintainrecord>();

        if(maintainrecord.getMaintenanceNum() != null  && !"".equals(maintainrecord.getMaintenanceNum() + "")) {
            queryWrapper = queryWrapper.like("maintenance_num",maintainrecord.getMaintenanceNum());
         }


        if(maintainrecord.getMaintainapplyId() != null  && !"".equals(maintainrecord.getMaintainapplyId() + "")) {
            queryWrapper = queryWrapper.like("maintainapply_id",maintainrecord.getMaintainapplyId());
         }


        if(maintainrecord.getEquId() != null  && !"".equals(maintainrecord.getEquId() + "")) {
            queryWrapper = queryWrapper.like("equ_id",maintainrecord.getEquId());
         }


        if(maintainrecord.getEquName() != null  && !"".equals(maintainrecord.getEquName() + "")) {
            queryWrapper = queryWrapper.like("equ_name",maintainrecord.getEquName());
         }


        if(maintainrecord.getEquType() != null  && !"".equals(maintainrecord.getEquType() + "")) {
            queryWrapper = queryWrapper.like("equ_type",maintainrecord.getEquType());
         }


        if(maintainrecord.getEquStatus() != null  && !"".equals(maintainrecord.getEquStatus() + "")) {
            queryWrapper = queryWrapper.like("equ_status",maintainrecord.getEquStatus());
         }


        if(maintainrecord.getEquUpdatetime() != null  && !"".equals(maintainrecord.getEquUpdatetime() + "")) {
            queryWrapper = queryWrapper.like("equ_updatetime",maintainrecord.getEquUpdatetime());
         }


        if(maintainrecord.getServicemanId() != null  && !"".equals(maintainrecord.getServicemanId() + "")) {
            queryWrapper = queryWrapper.like("serviceman_Id",maintainrecord.getServicemanId());
         }


        if(maintainrecord.getFaultCause() != null  && !"".equals(maintainrecord.getFaultCause() + "")) {
            queryWrapper = queryWrapper.like("fault_cause",maintainrecord.getFaultCause());
         }

        IPage<Maintainrecord> pageInfo = maintainrecordService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  maintainrecord);
        map.put("pageInfo", new PageInfo(pageInfo));

        return this.success(map);
    }


    /**
    * 添加
    * @param maintainrecord
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(Maintainrecord maintainrecord){
        return toAjax(maintainrecordService.save(maintainrecord));
    }

    /**
    * 添加
    * @param maintainrecord
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(Maintainrecord maintainrecord){
        return toAjax(maintainrecordService.updateById(maintainrecord));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(maintainrecordService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(maintainrecordService.removeByIds(ids));
    }

}

